<?php
namespace app\shop\controller;
use app\common\controller\Common;
use app\shop\validate\ConsultValidate;
use app\shop\model\ConsultModel;
use app\common\model\Category;
use app\common\model\Shop;
use think\Exception;
use think\Request;
use think\Session;

/**
* 咨询单
*/
class Consult extends Common
{
	/**
    * 添加咨询单
    */
    public function addconsult()
    {
        if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return json(ConsultModel::addConsult($info));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
        $goodssn = time() . Session::get('admin.shop_id') . rand(100,999);
        $this->assign('shopname',(new Shop())->shopField(['shop_id'=>Session::get('admin.shop_id')],'shop_name'));
        $this->assign('sn',$goodssn);
        return $this->fetch();
    }
    
    /**
    * 添加商品信息
    */
    public function addconsgoods()
    {
        $info = Request::instance()->param();
        if(request()->isPost())
		{
			try{
				 $ConsultValidate = new ConsultValidate();
				 if(!$ConsultValidate->scene('addconsgoods')->check($info))
				 {
					return json(back(config('status.STATUS_CODE_SIX'),$ConsultValidate->getError()));
				 }
                return json(ConsultModel::addConGoods($info));
            }catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
        $this->assign('sn',$info['sn']);
        $this->assign('fcat',(new Category())->categoryList(['fid'=>0],'category_id,category_name'));
        return $this->fetch();
    }
    
    /**
    * 商品列表
    */
    public function consgoodslist()
    {
        if(request()->isPost())
		{
			$info = Request::instance()->param();
			return ConsultModel::ConsGoodsList($info);
		}
    }
    
    /**
    * 删除商品信息
    */
    public function delgoods()
    {
        if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return json(ConsultModel::delGoods($info['id']));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
    }
    
    /**
    * 咨询管理
    */
    public function consultlist()
    {
        if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return json(ConsultModel::getConsultList($info));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
        $this->assign('fcat',(new Category())->categoryList(['fid'=>0],'category_id,category_name'));
        return $this->fetch();
    }
    /**
    * 咨询详情
    */
    public function consultinfo()
    {
        $info = Request::instance()->param();
        list($info,$goods,$img) = ConsultModel::consultInfo($info);
        $this->assign('info',$info);
        $this->assign('goods',$goods);
        $this->assign('img',$img);
        return $this->fetch();
    }
}
